

//
//  ZFBPayManager.m
//  YYPHSpeedJump
//
//  Created by sujiang on 16/1/8.
//  Copyright © 2016年 sujiang. All rights reserved.
//

#import "ZFBPayManager.h"

@implementation ZFBPayManager

+ (ZFBPayManager *)shareManager
{
    static ZFBPayManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (!manager) {
            manager = [[self alloc] init];
        }
    });
    return manager;
}


- (void)alipayWithOrderProductName:(NSString *)productName notifyUrl:(NSString *)url productDescription:(NSString *)proDescr money:(NSString *)money paymentType:(NSString *)paymentType tradeNO:(NSString *)tradeNO
{
    //    NSLog(@"调起支付宝");
    
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *partner = @"2088711790421992";//
    NSString *seller = @"speedjump@qq.com";//
    NSString *privateKey = @"MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAL7cyOMAMowD3xzGcUfQ6A+mb1bzNk0kw5BzmAZFKK84J9XRf8fQyME+yWfVtP80+AvICH4jTV36U1GI0CTnWyocyjMuhXlztKa3wIVPTLWCUyA0YMHCpyuq0uKLz3WTnc3Ughbpj/oxkwpFqpzAr3tB3IKjsiSgkzQ7J7wF3WWNAgMBAAECgYB6zWW3oK3lJqyMQCYs1f4p66VhpZT/p7Loo2diSWc27gO7cFMuBmjU5YgC9h7BwZwDQI4uUpp56BFpuYRq6izd2+rgFUhRIPy3WyVhEtfMz0o4/G1QsTk2CPq0YnPeQrL96CqFzzdEk2+TdymHVpUbHQiLkGeS71Lt3jlbV3s0BQJBAOeykrYU0UKflm93YPbEhSHdeXaGh+NCtZ6TN/IwPox/2Y+vUyqUwOmwowC4GFJzwwNEkMQrsmt4XylRLErn07MCQQDS4bpFjLif1Qqyon68sAqxZKgaib8yXeDkVsCpWDP7lJRq2ZzsRXG2pQNXNAJf9qgf+RW3m1vnIOJPUA2GQkG/AkAOvESoX9qPKicfH7CDASyWYsd+yj+KLIvwcdx1Aqx3swnbIYNgieVa92jmes7RL5uPcaTjVQQb4aPrtMXkXaoRAkAPs9867o+Q1zxEsPTe9/zT2LrrvvYNug/U8ODHu1mjkhy7GBknPsSI5SCqjaTfoG5Ecze6xEbDnx866KsZJDXhAkEAxMkNAnq/jQZWQg4dAT8X6FCcRZLXNEA+WQLac64l/AE3W7fQC0pjfb8Z8Rs2xYsztZMeDJwBkAGb+HHkApV5Yg==";
    
    /*============================================================================*/
    
    
    //partner和seller获取失败,提示
    if ([partner length] == 0 || [seller length] == 0)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少partner或者seller。"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order *order = [[Order alloc] init];
    order.partner = partner;
    order.seller = seller;
    order.tradeNO = tradeNO; //订单ID（由商家自行制定）    ///
    order.productName = productName; //商品标题
    order.productDescription = proDescr; //商品描述
    
    order.amount = money;//_CreatOrderMonay; //商品价格                //
    order.notifyURL = url; //回调URL
    
    //    @"http://112.74.80.14:8081/alipay/notify_url.php";
    
    //    [NSString stringWithFormat:@"http://112.74.80.14:8081/zhifu.php&token=%@&order_sn=%@&type=%@",Token,_creatOrderId,@"3"]; //回调URL
    //    BASE_URL(@"/mobile/index.php?m=default&c=order&a=backpay")
   // NSLog(@"%@",order.notifyURL);
    
    order.service = @"mobile.securitypay.pay";
    order.paymentType = paymentType;
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"SJAliPay";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    NSString *signedString = [signer signString:orderSpec];
    NSLog(@"signer:%@",signer);
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        //网页支付回调
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            // NSLog(@"resultDic = %@",resultDic);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Alipay" object:nil userInfo:resultDic];
        }];
    }

}






@end
